// manager/pages/gridbuildingdesc/index.js
import config from '../../../utils/config';
import { getSystemInfo } from '../../../utils/getSystemInfo';
import { request } from '../../../utils/request';
import util from '../../../common/util';
import WxValidate from '../../../common/WxValidate';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        entLocationId: "",
        merchantType: "",
        params: {
            search: "",
            industry: "",
        },
        // 行业字典
        industryList: [],
        industryIndex: -1,
        scroller: {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.name){
            wx.setNavigationBarTitle({
                title: options.name
            })
        }
        this.setData({
            entLocationId: options.id,
            merchantType: options.merchantType,
        })
        this.getAllEnterpriseListByLocation();
        this.getDictionaryInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 获取企业
     */
    getAllEnterpriseListByLocation() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/enterpriseMarketing/getAllEnterpriseListByLocation`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                entLocationId: this.data.entLocationId,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /**
     * 获取字典
     */
    getDictionaryInfo() {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
                dictType: 'ecard_enterprise_industry'
            }
        }).then(res => {
            if(res.code == 0){
                let industryList = [];
                industryList.push({
                    dictLabel: "全部",
                    dictValue: ""
                })
                this.setData({
                    industryList: industryList.concat(res.data)
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 输入框输入时
     */
    input(e){
        let key = e.target.dataset.key;
        this.setData({
            ['params.'+key]: e.detail.value,
            // isFilter: !!e.detail.value,
        })
    },
    bindPickerChange(e){
        this.setData({
            industryIndex: e.detail.value,
            'params.industry': this.data.industryList[e.detail.value].dictValue
        })
    },
    // 搜索
    searchConfirm(e){
        console.log(e.detail.value)
        this.setData({
            'params.search': e.detail.value
        })
        // 页面加载时执行一次加载
		this.resetScroller()
        this.getAllEnterpriseListByLocation();
    },
    searchBtn(e){
        this.resetScroller()
        this.getAllEnterpriseListByLocation();
    },

    /**
     * 重置下拉
     */
    resetScroller(){
        let scroller = {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
		}
		this.setData({
            scroller,
		})
    },

    refresh: function () {
        this.resetScroller()
        this.getAllEnterpriseListByLocation()
    },
    
	loadMore: function () {
        console.log("loadMore")
		this.getAllEnterpriseListByLocation()
    },

    /**
     * 跳转不可编辑拜访记录
     */
    toGridvisit(e) {
        let { id, type } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/manager/pages/gridvisit/index?merchantId=${id}&merchantType=${type}`,
        })
    },
})